package com.oocl.connection;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class MQConnection {
	private boolean isStarted = false;
	
	private static MQConnection sInstance = new MQConnection();
	
	private Connection con;
	
	private MQConnection(){
		
	}
	
	public static MQConnection getInstance(){
		return sInstance;
	}
	
	public void start(){
		if(this.isStarted)
			return;
		
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616/");
		try {
			con = factory.createConnection();
			con.start();
			this.isStarted = true;
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	public Session getSession(){
		try {
			return con.createSession(false, Session.CLIENT_ACKNOWLEDGE);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	public void close(){
		try {
			this.con.close();
			this.isStarted = false;
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
